#include "modifypricediscount.h"
#include "ui_modifypricediscount.h"
#include "saledetail.h"
#include <QKeyEvent>
#include "inquiresql.h"
#include <QMessageBox>
#include <QRegExpValidator>

ModifyPriceDiscount::ModifyPriceDiscount(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModifyPriceDiscount)
{
    ui->setupUi(this);
    this->setLayout(ui->MainLayout);
    this->setFixedSize(280,352);
    setWindowTitle(tr("修改价格/折扣"));

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    m_lastClick = -1;
    ui->labelOriginalPrice->setDisabled(true);
    ui->pushButtonConfirm->setFlat(true);
    ui->pushButtonQuit->setFlat(true);
//    m_forbidModifyPriceDiscount = false;
//    setMode();

    QRegExp regExpDiscount("[0-9]{1,2}|100");
    QRegExpValidator *pRegExpValidatorDiscount = new QRegExpValidator(regExpDiscount, this);
    ui->lineEditDiscount->setValidator(pRegExpValidatorDiscount);

    QRegExp regExpPrice(" [0-9]{0,6}|[0-9]{0,6}[.]{1}[0-9]{0,2} ");
    QRegExpValidator *pRegExpValidatorPrice = new QRegExpValidator(regExpPrice, this);
//    ui->labelOriginalPrice->setValidator(pRegExpValidatorPrice);
    ui->lineEditSubsum->setValidator(pRegExpValidatorPrice);

    QRegExp regExpQuantity("[0-9]*");
    QRegExpValidator *pRegExpValidatorQuantity = new QRegExpValidator(regExpQuantity, this);
    ui->lineEditSaleQuantity->setValidator(pRegExpValidatorQuantity);

    //添加时间过滤器
    ui->lineEditSaleQuantity->installEventFilter(this);
    ui->lineEditDiscount->installEventFilter(this);
    ui->lineEditSellingPrice->installEventFilter(this);
    ui->lineEditSubsum->installEventFilter(this);
    ui->pushButtonConfirm->installEventFilter(this);
}

ModifyPriceDiscount::~ModifyPriceDiscount()
{
    delete ui;
}

void ModifyPriceDiscount::displaySaleCommodityInformation(const SaleDetail &saleDetail)
{
    QString price = QString::number(saleDetail.get_sellingPrice().toDouble() *
                saleDetail.get_discount().toDouble() / 100, 'd', 2);

    ui->labelCommodityBarcode->setText(saleDetail.get_commodityBarcode());
    ui->labelCommodityName->setText(saleDetail.get_commodityName());
    ui->labelCurrentStock->setText(saleDetail.get_stockQuantity());
    ui->lineEditDiscount->setText(saleDetail.get_discount());
    ui->lineEditSaleQuantity->setText(saleDetail.get_saleQuantity());
    ui->labelOriginalPrice->setText(saleDetail.get_sellingPrice());
    ui->lineEditSubsum->setText(saleDetail.get_saleSum());
//    ui->lineEditSellingPrice->setText(price);
    ui->lineEditSellingPrice->setText(saleDetail.get_realSellingPrice());
}

bool ModifyPriceDiscount::eventFilter(QObject *target, QEvent *event)
{
    if ( target == ui->lineEditSaleQuantity || target == ui->lineEditDiscount
      || target == ui->lineEditSellingPrice || target == ui->lineEditSubsum
      || target == ui->pushButtonConfirm )
    {
        if (event->type() == QEvent::FocusOut)
        {
            if (target == ui->lineEditSaleQuantity)
            {
                ui->lineEditSaleQuantity->setText(QString::number(ui->lineEditSaleQuantity->text().toInt()));
                return true;
            }
        }
        //鼠标左键按下事件
        if (event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)
            {
                if (target == ui->lineEditSaleQuantity)
                {
                    if (m_lastClick != lineEditSaleQuantity){
                        m_lastClick = lineEditSaleQuantity;
                        ui->lineEditSaleQuantity->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditSaleQuantity)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditDiscount)
                {
                    if (m_lastClick != lineEditDiscount){
                        m_lastClick = lineEditDiscount;
                        ui->lineEditDiscount->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditDiscount)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditSellingPrice)
                {
                    if (m_lastClick != lineEditSellingPrice){
                        m_lastClick = lineEditSellingPrice;
                        ui->lineEditSellingPrice->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditSellingPrice)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditSubsum)
                {
                    if (m_lastClick != lineEditSubsum){
                        m_lastClick = lineEditSubsum;
                        ui->lineEditSubsum->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditSubsum)
                    {
                        goto flag;
                    }
                }
                if (target == ui->pushButtonConfirm)
                {
                    m_lastClick = pushButtonConfirm;
                    goto flag;
                }
            }
        }

        //键盘事件
        if (event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if (keyEvent->key() == Qt::Key_Escape)
            {
                this->hide();
                return true;
            }
            if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
            {
                if (target == ui->lineEditSaleQuantity)
                {
                    ui->lineEditDiscount->setFocus();
                    ui->lineEditDiscount->selectAll();
                    return true;
                }
                if (target == ui->lineEditDiscount)
                {
                    ui->lineEditSellingPrice->setFocus();
                    ui->lineEditSellingPrice->selectAll();
                    return true;
                }
                if (target == ui->lineEditSellingPrice)
                {
                    ui->lineEditSubsum->setFocus();
                    ui->lineEditSubsum->selectAll();
                    return true;
                }
                if (target == ui->lineEditSubsum)
                {
                    ui->pushButtonConfirm->setFocus();
                    return true;
                }
                if (target == ui->pushButtonConfirm)
                {
                    on_pushButtonConfirm_clicked();
                }
            }
        }
    }
flag:
    return QWidget::eventFilter(target, event);
}

void ModifyPriceDiscount::on_pushButtonQuit_clicked()
{
    this->hide();
}

void ModifyPriceDiscount::on_pushButtonConfirm_clicked()
{
    if (ui->lineEditSaleQuantity->text().toInt() > ui->labelCurrentStock->text().toInt())
    {
        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                             tr("\n此产品库存不足,请重新设置销售数量!") );
        ui->lineEditSaleQuantity->setText(ui->labelCurrentStock->text());
        ui->lineEditSaleQuantity->setFocus();
        ui->lineEditSaleQuantity->selectAll();
        return;
    }
    SaleDetail temp(ui->labelCommodityBarcode->text(),
                    ui->lineEditDiscount->text(),
                    ui->lineEditSaleQuantity->text(),
                    ui->lineEditSellingPrice->text(),
                    ui->lineEditSubsum->text());
    emit updateSalePriceDiscount(temp);
    this->hide();
}

void ModifyPriceDiscount::setMode()
{
    if (m_forbidModifyPriceDiscount)
    {
        ui->lineEditDiscount->setReadOnly(true);
        ui->lineEditSellingPrice->setReadOnly(true);
        ui->lineEditSubsum->setReadOnly(true);

        ui->lineEditDiscount->setStyleSheet(
            " QLineEdit{background: white; color:gray;} ");
        ui->lineEditSellingPrice->setStyleSheet(
            " QLineEdit{background: white; color:gray;} ");
        ui->lineEditSubsum->setStyleSheet(
            " QLineEdit{background: white; color:gray;} ");
    }
    else
    {
        ui->lineEditDiscount->setReadOnly(false);
        ui->lineEditSellingPrice->setReadOnly(false);
        ui->lineEditSubsum->setReadOnly(false);

        ui->lineEditDiscount->setStyleSheet(
            " QLineEdit:Disabled {color:gray} "
            " QLineEdit:hover{background-color:rgb(250,250,150);} "
            " QLineEdit:focus {background-color:yellow;} "
            " QLineEdit:focus {color: red;} "
            " QLineEdit{background: white;color:blue;} ");
        ui->lineEditSellingPrice->setStyleSheet(
            " QLineEdit:Disabled {color:gray} "
            " QLineEdit:hover{background-color:rgb(250,250,150);} "
            " QLineEdit:focus {background-color:yellow;} "
            " QLineEdit:focus {color: red;} "
            " QLineEdit{background: white;color:blue;} ");
        ui->lineEditSubsum->setStyleSheet(
            " QLineEdit:Disabled {color:gray} "
            " QLineEdit:hover{background-color:rgb(250,250,150);} "
            " QLineEdit:focus {background-color:yellow;} "
            " QLineEdit:focus {color: red;} "
            " QLineEdit{background: white;color:blue;} ");
    }
}

void ModifyPriceDiscount::on_lineEditSaleQuantity_textChanged(const QString &)
{
    ui->lineEditSubsum->setText(completionPrice(QString::number(
                                ui->lineEditSaleQuantity->text().toInt()
                                * ui->lineEditSellingPrice->text().toDouble(), 'd', 2)));
}

void ModifyPriceDiscount::on_lineEditSellingPrice_textChanged(const QString &)
{
    ui->lineEditSubsum->setText(completionPrice(QString::number(
                                ui->lineEditSaleQuantity->text().toInt()
                                * ui->lineEditSellingPrice->text().toDouble(), 'd', 2)));
}

void ModifyPriceDiscount::on_lineEditDiscount_textChanged(const QString &)
{
    ui->lineEditSellingPrice->setText(completionPrice(QString::number(
                                ui->labelOriginalPrice->text().toDouble()
                                * ui->lineEditDiscount->text().toDouble() / 100, 'd', 2)));
    ui->lineEditSubsum->setText(completionPrice(QString::number(
                                ui->lineEditSaleQuantity->text().toInt()
                                * ui->lineEditSellingPrice->text().toDouble(), 'd', 2)));
}

QString ModifyPriceDiscount::completionPrice(const QString &price)
{
    QString number = price;
    if (number.isEmpty())
    {
        number.append("0.00");
        return number;
    }
    if (number.contains(".", Qt::CaseInsensitive))
    {
        if (number.left(1) == ".")
        {
            number.replace(0, 1, "0.");
            return number;
        }
        if (number.right(1) == ".")
        {
            number.append("00");
            return number;
        }
        if (number.right(2).contains(".", Qt::CaseInsensitive))
        {
            number.append("0");
            return number;
        }
    }
    else
    {
        number.append(".00");
        return number;
    }

    return NULL;
}

void ModifyPriceDiscount::on_lineEditSubsum_editingFinished()
{
    ui->lineEditSubsum->setText(completionPrice(ui->lineEditSubsum->text()));
}

void ModifyPriceDiscount::on_lineEditSellingPrice_editingFinished()
{
    ui->lineEditSellingPrice->setText(completionPrice(ui->lineEditSellingPrice->text()));
}

